import base64
import hashlib
import os
import random
import re
import uuid

import requests
import threading
import time
import string
import json
from functools import wraps

from flask import request, session, Response, Blueprint, jsonify, make_response, render_template, g
from flask_mail import Message

# from app.service.imageFunction import gene_code
# from OperationPlatform.settings import BASE_DIR
# from app.service.sendMailFunction import eMailClass
from sqlalchemy import desc, asc

from app.utils.authority import exception_handling
from .models import *
from app import app, db
from exts import _isLogin, mail, scheduler
from app.utils.error import ResponseError
# from app.utils.apSheduler import APScheduler

from app.utils.time_util import *
from app.utils.apscheduler_util import *

Task_api = Blueprint('task_api', __name__, template_folder='templates')


@Task_api.route('/history.do', methods=['post'])
@exception_handling
@_isLogin
def task_get_job_history_do():
    jobs = QJobHistory.query.all()
    return jsonify(data=list(map(lambda x: x.to_dict(), jobs)))


@Task_api.route('/run_history.do', methods=['post'])
@exception_handling
@_isLogin
def task_get_run_history_do():
    tasks = QTaskHistory.query.order_by(desc('time')).all()
    return jsonify(data=list(map(lambda x: x.to_dict(), tasks)))


@Task_api.route('/run_history_by_id.do', methods=['post'])
@exception_handling
@_isLogin
def task_get_run_history_by_id_do():
    _id = request.values.get('id')
    tasks = QTaskHistory.query.filter_by(task_id=_id).order_by(desc('time')).all()
    return jsonify(data=list(map(lambda x: x.to_dict(), tasks)))
